package com.banzhi.shop.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.model.CallBack.FindPasswordCallBack;
import com.banzhi.shop.model.CallBack.SendCodeCallBack;
import com.banzhi.shop.model.IFindPasswordModel;
import com.banzhi.shop.model.ISendCodeModel;
import com.banzhi.shop.model.Impl.FindPasswordImpl;
import com.banzhi.shop.model.Impl.SendCodeImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

public class ForgetPasswordActivity extends BaseActivity implements View.OnClickListener, SendCodeCallBack, FindPasswordCallBack {
    private ImageView ivback;
    private EditText etusername;
    private EditText etcode;
    private EditText etpassword;
    private EditText etpasswordtwo;
    private TextView tvsendcode;
    private Button btntrue;
    private Timer timer;
    private String phone;
    private String pwd;
    private String pwdtwo;
    private String code;
    private int number = 60;
    private ISendCodeModel sendCodeModel;
    private IFindPasswordModel model;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    number--;
                    tvsendcode.setText(number + "");
                    if (number < 0) {
                        tvsendcode.setClickable(true);
                        tvsendcode.setText("发送验证码");
                        number = 60;
                        timer.cancel();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        Translucent.setTranslucent(this);
        initsViews();
        setListeners();
        sendCodeModel = new SendCodeImpl();
        model = new FindPasswordImpl();
    }

    private void initsViews() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        etusername = (EditText) findViewById(R.id.et_name);
        etcode = (EditText) findViewById(R.id.et_code);
        etpassword = (EditText) findViewById(R.id.et_password);
        etpasswordtwo = (EditText) findViewById(R.id.et_passwordtwo);
        tvsendcode = (TextView) findViewById(R.id.tv_send_code);
        btntrue = (Button) findViewById(R.id.btn_true);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        tvsendcode.setOnClickListener(this);
        btntrue.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_send_code:
                phone = etusername.getText().toString().trim();
                if (!ASUtils.isCanUse(phone)) {
                    ToastUtils.makeToastShort("请先输入手机号码");
                } else if (phone.length() != 11) {
                    ToastUtils.makeToastShort("手机号码格式不正确");
                } else {
                    LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                    sendCodeModel.getData(phone, this);
                }
                break;
            case R.id.btn_true:
                phone = etusername.getText().toString().trim();
                pwd = etpassword.getText().toString().trim();
                pwdtwo = etpasswordtwo.getText().toString().trim();
                code = etcode.getText().toString().trim();
                if (!ASUtils.isCanUse(phone)) {
                    ToastUtils.makeToastShort("请先输入您的手机号");
                } else if (!ASUtils.isCanUse(code)) {
                    ToastUtils.makeToastShort("请先输入验证码");
                } else if (!ASUtils.isCanUse(pwd)) {
                    ToastUtils.makeToastShort("请先输入密码");
                } else if (!ASUtils.isCanUse(pwdtwo)) {
                    ToastUtils.makeToastShort("请再次输入密码");
                } else if (!ASUtils.isCanUse(pwdtwo)) {
                    ToastUtils.makeToastShort("请再次输入密码");
                } else {
                    LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                    model.findPassword(phone, pwd, pwdtwo, code, this);
                }
                break;
        }
    }

    @Override
    public void sendCodeSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        tvsendcode.setClickable(false);
        if (timer == null) {
            timer = new Timer();
        } else {
            timer.cancel();
            timer = new Timer();
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message msg = handler.obtainMessage();
                msg.what = 100;
                handler.sendMessage(msg);
            }
        }, 0, 998);
    }

    @Override
    public void sendCodeError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void ongetDataSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        finish();
    }

    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
